package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.WxKfService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.WxKfForm;
import com.youlai.boot.system.model.query.WxKfQuery;
import com.youlai.boot.system.model.vo.WxKfVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 客户档案前端控制层
 *
 * @author youlaitech
 * @since 2025-08-26 15:58
 */
@Tag(name = "客户档案接口")
@RestController
@RequestMapping("/api/v1/wxKfs")
@RequiredArgsConstructor
public class WxKfController  {

    private final WxKfService wxKfService;

    @Operation(summary = "客户档案分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:wxKf:query')")
    public PageResult<WxKfVO> getWxKfPage(WxKfQuery queryParams ) {
        IPage<WxKfVO> result = wxKfService.getWxKfPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增客户档案")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:wxKf:add')")
    public Result<Void> saveWxKf(@RequestBody @Valid WxKfForm formData ) {
        boolean result = wxKfService.saveWxKf(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取客户档案表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:wxKf:edit')")
    public Result<WxKfForm> getWxKfForm(
        @Parameter(description = "客户档案ID") @PathVariable Long id
    ) {
        WxKfForm formData = wxKfService.getWxKfFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改客户档案")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:wxKf:edit')")
    public Result<Void> updateWxKf(
            @Parameter(description = "客户档案ID") @PathVariable Long id,
            @RequestBody @Validated WxKfForm formData
    ) {
        boolean result = wxKfService.updateWxKf(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除客户档案")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:wxKf:delete')")
    public Result<Void> deleteWxKfs(
        @Parameter(description = "客户档案ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = wxKfService.deleteWxKfs(ids);
        return Result.judge(result);
    }
}
